package com.file;

import java.io.*;

public class File {
    String path;

    public File(String path) {
        this.path = path;
    }

    public String readToString() {
        String fileString = "";
        String line = "";
        try {
            BufferedReader input = new BufferedReader(new FileReader(this.path));
            while ((line = input.readLine()) != null) {
                if(fileString != "") {
                    fileString += '\n';
                }
                fileString += line;
            }
        } catch (IOException e) {
            System.out.print(e);
        }

        return fileString;
    }

    public boolean writeString(String contennt) {
        try {
            BufferedWriter output = new BufferedWriter(new FileWriter(this.path, true));
            output.write(contennt);
            output.close();
        } catch (IOException e) {
            System.out.print(e);
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        File file = new File("test.txt");
        String fileString = file.readToString();
        System.out.println("读取字符串如下：" + fileString);
        boolean writeResult = file.writeString("这个是写入的内容");
        String newFileString = file.readToString();
        System.out.println("读取字符串如下：" + newFileString);
    }
}
